﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dbPoll_Application.Models.MultipleChoiceQuestion>" %>
<%@ Import Namespace="dbPoll_Application.Helpers" %>

    <div id="questionEditorContainer">
        <%= Html.HiddenFor(m => m.questionID)%>
        <%= Html.HiddenFor(m => m.creationTime)%>
        <%= Html.HiddenFor(m => m.deletionTime)%>
        <%= Html.HiddenFor(m => m.type)%>
        <%= Html.HiddenFor(m => m.questionNumber)%>
        
        <ul id="editMCQTable">
            <li id="questionRow"><%= Html.TextAreaFor(m => m.question)%></li>
        </ul>
        <div id="optionContainer">
        <ul id="optionList">
        <li class="optionsHeader">
            <span class="optionControls">
                <%  var pollRoles = ViewData["pollRoles"] as ICollection<dbPoll_Application.Models.Role>;
                    var pollCreator = new dbPoll_Application.Models.Role("Poll Creator");
                    var pollAdministrator = new dbPoll_Application.Models.Role("Poll Administrator"); 
                    DateTime? pollClose = ViewData["pollClose"] as DateTime?;
                    DateTime? pollStart = ViewData["pollStart"] as DateTime?;
                    if (Model.type != dbPoll_Application.Models.QuestionType.Demographic)
                   { %>
                <span class="pointsHeader">Points</span>
                <span class="correctHeader">Correct</span>
                <% } %>
            </span>
        </li>
        <% for (int i = 0; i < Model.options.Count; i++) 
           { %>
               <li class="option">
                   <span class="optionNumber"><%= i + 1%>.</span>
                   <span class="optionControls">
                       <% if (Model.type != dbPoll_Application.Models.QuestionType.Demographic)
                           { %>
                       <span><%= Html.TextBoxFor(m => m.options[i].pointsCurrent, new { @class = "pointsCurrent" })%><%= Html.HiddenFor(m => m.options[i].pointsOriginal)%></span>
                       <span><%= Html.CheckBoxFor(m => m.options[i].correctCurrent, new { @class = "correctCurrent" })%><%= Html.HiddenFor(m => m.options[i].correctOriginal)%></span>
                       <% }
                          else
                          { %>
                       <span><%= Html.HiddenFor(m => m.options[i].pointsCurrent)%><%= Html.HiddenFor(m => m.options[i].pointsOriginal)%></span>
                       <span><%= Html.HiddenFor(m => m.options[i].correctCurrent)%><%= Html.HiddenFor(m => m.options[i].correctOriginal)%></span>
                       <% } %>
                       <span><%= Html.HiddenFor(m => m.options[i].optionID) %><%= Html.HiddenFor(m => m.options[i].optionNumber) %></span>
                       <% if ((pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) && pollStart==null)
                          { %><span class="optionUp"><img src="../../Resources/uparrow.png" /></span>
                       <span class="deleteOption"><img src="../../Resources/remove.png" /></span><%}
                              else {%><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><%} %>
                   </span>
                   <span class="optionText"><%= Html.TextAreaFor(m => m.options[i].answer, new { @class = "optionTextarea" })%></span>
               </li>
        <% } %>
        </ul>
        <% if (((pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) && (pollStart==null)) || ViewData["testPoll"] as String == "True")
           { %><textarea cols="1" rows="20" id="addOptionText"></textarea>
        <% if (Model.options.Count < 10)
           { %>
        <input type="button" value="Add Option" id="addOptionButton" />
        <% }
           else
           { %>
                <input type="button" value="Add Option" id="addOptionButton" disabled="true" />
        <% }
        } %>
        </div>
        <div id="buttons">
            <ul>
               <%  var saved = ViewData["saved"] as String;
                   if (saved == "true") { %>
                   <li class="left"><input type="submit" value="Saved" id="submitForm" disabled="disabled" /><span id="result"></span><span id="errorMessage"></span><span><%= Html.ValidationMessageFor(m => m.question)%></span></li>
                   <script type="text/javascript">
                       $(document).click(function() {
                           $('#submitForm').val("Save");
                           $('#submitForm').attr("disabled", false);
                           $(document).unbind('click');
                       });
                   </script>
                   <% } 
                      else { %>
               <li class="left"><input type="submit" value="Save" id="submitForm" /><span id="result"></span><span id="errorMessage"></span><span><%= Html.ValidationMessageFor(m => m.question)%></span></li>
               <% } %>
               <% if (Model.type != dbPoll_Application.Models.QuestionType.Demographic)
                  { %>
               <li class="right"><label for="allowedAnswers">answers allowed: </label><%= Html.DropDownListFor(m => m.allowedAnswers, new SelectList(Enumerable.Range(1, 5)))%></li>
               <% }
                  else
                  { %>
               <li class="right"><%= Html.HiddenFor(m => m.allowedAnswers)%></li>
               <% } %>
               <li class="right"><label for="asynchronous">asynchronous: </label><%= Html.CheckBoxFor(m => m.asynchronous, new { id = "asynchronous" })%></li>
            </ul>
        </div>
    </div>
    <% var a = ViewData["testPoll"];
       var b = a; %>
    <script type="text/javascript" >
        <%if  ((!(pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) || pollStart!=null) && ViewData["testPoll"] as String == "False") 
          {%>document.getElementById("question").readOnly=true;
        <%   int it=0;
             foreach (var options in Model.options)
             {%>
                document.getElementById("options_<%=it%>__answer").readOnly=true;
                document.getElementById("allowedAnswers").disabled=true;
                if ("<%= pollClose>DateTime.Now%>"=="True")
                {
                    document.getElementById("options_<%=it%>__pointsCurrent").readOnly=true;
                    document.getElementById("options_<%=it%>__correctCurrent").disabled=true;          
                }
                <%it++;
             }
          } %>
    </script>